package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 使用预编译sql执行DML语句
 */
public class JDBCDemo8 {
    public static void main(String[] args) {
        /*Userinfo userinfo = InputUtil.getInputObject(new Userinfo(), "欢迎注册", "注册");
        System.out.println(userinfo);

        try(Connection connection = DBUtil.getConnection();) {
            String sql = "INSERT INTO userinfo(username,password,nickname,age) " +
                    "VALUES(?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,userinfo.getUsername());
            ps.setString(2,userinfo.getPassword());
            ps.setString(3,userinfo.getNickname());
            ps.setInt(4,userinfo.getAge());
            int rows = ps.executeUpdate();
            if(rows>0){
                System.out.println("注册成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }*/

        Userinfo userinfo = InputUtil.getInputObject(new Userinfo(),"欢迎注册","注册");
        System.out.println(userinfo);

        try(Connection connection = DBUtil.getConnection();) {
            String sql = "INSERT INTO userinfo(username,password,nickname,age)VALUES(?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,userinfo.getUsername());
            ps.setString(2,userinfo.getPassword());
            ps.setString(3,userinfo.getNickname());
            ps.setInt(4,userinfo.getAge());
            int rows = ps.executeUpdate();
            if(rows>0){
                System.out.println("插入成功");
            }else{
                System.out.println("插入失败");
            }
        } catch (SQLException e) {

            e.printStackTrace();
        }


    }
}
